package com.xm.xmds1705083.service.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.base.Strings;
import com.xm.xm.dto.ResultDto;
import com.xm.xm.dto.SearchDto;
import com.xm.xm.util.DateUtils;
import com.xm.xm.util.ErrorCode;
import com.xm.xm.util.ValidateUtils;
import com.xm.xmds1705083.service.mapper.DuoduoUserMapper;
import com.xm.xmds1705083.service.mapper.DuoduoUserSpaceMapper;
import com.xm.xmds1705083.service.mapper.DuoduoCertfCartportMapper;
import com.xm.xmds1705083.service.model.DuoduoCertfCartport;
import com.xm.xmds1705083.service.model.DuoduoUser;
import com.xm.xmds1705083.service.model.DuoduoUserSpace;
import com.xm.xmds1705083.service.service.DuoduoUserSpaceService;
import com.xm.xmds1705083.service.util.DateUtil;
import com.xm.xmds1705083.service.util.GlobalError;
import lombok.Data;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 杭州新苗网络科技有限公司
 *
 * @author meihf
 * @date 2017/06/14
 * @description
 */
@Service("duoduoUserSpaceService")
public class DuoduoUserSpaceServiceImpl extends ServiceImpl<DuoduoUserSpaceMapper, DuoduoUserSpace> implements DuoduoUserSpaceService {

    @Autowired
    DuoduoUserSpaceMapper duoduoUserSpaceMapper;

    @Autowired
    DuoduoUserMapper duoduoUserMapper;

    @Autowired
    DuoduoCertfCartportMapper   duoduoCertfCartportMapper;

    @Override
    public ResultDto findMyParking(SearchDto searchDto) {
        ResultDto resultDto = new ResultDto();
        searchDto.setPageNo((searchDto.getPageNo() - 1) * searchDto.getPageSize());
        JSONObject filed = searchDto.getFiled();
        try {
//             if (!filed.has("phone") || !filed.has("name")){
//                  resultDto.setMsg(ErrorCode.E10011);
//                  resultDto.setCode(ErrorCode.C10011);
//                  return resultDto;
//              }else{
                RowBounds rowBounds = new RowBounds(searchDto.getPageNo(), searchDto.getPageSize());
                 List<DuoduoUserSpace> duoduoUserSpaceList = duoduoUserSpaceMapper.selectPage(rowBounds, new EntityWrapper<DuoduoUserSpace>()
                         .where("userId={0} and isDelete=0",searchDto.getAppUserId()).orderBy("createTime", false));

             if (duoduoUserSpaceList.size() == 0) {
                 resultDto.setCode(ErrorCode.C11001);
                 resultDto.setMsg(ErrorCode.E11001);
                 return resultDto;
             }
             JSONArray userSpaceArr = JSONArray.fromObject(duoduoUserSpaceList);
             userSpaceArr.forEach((userSpace) -> {
                 ((JSONObject) userSpace).put("startTime", DateUtil.timeStamp2Date(((JSONObject) userSpace).getLong("startTime"), DateUtils.PATTERN_DATE_TIME));
                 ((JSONObject) userSpace).put("endTime", DateUtil.timeStamp2Date(((JSONObject) userSpace).getLong("endTime"), DateUtils.PATTERN_DATE_TIME));
             });
             resultDto.setData(userSpaceArr);
             resultDto.setMsg("查询成功！");
//              }

        } catch (RuntimeException e) {
            e.printStackTrace();
            resultDto.setCode(ErrorCode.C10013);
            resultDto.setMsg(ErrorCode.E10013);
        }
        return resultDto;
    }



    @Override
    public ResultDto certfMyParking(SearchDto searchDto) {
        //现在一期认证车位给的是假数据
        ResultDto resultDto = new ResultDto();
        searchDto.setPageNo((searchDto.getPageNo() - 1) * searchDto.getPageSize());
        JSONObject filed = searchDto.getFiled();
        String phone = filed.getString("phone");
        String name = filed.getString("name");
        String myIdCard=filed.getString("myIdCard");
        try {
            if (!filed.has("phone") ||!filed.has("name")||!filed.has("myIdCard")){
                resultDto.setMsg(ErrorCode.E10011);
                resultDto.setCode(ErrorCode.C10011);
            }
            DuoduoUserSpace duoduoUserSpace = new DuoduoUserSpace();
            duoduoUserSpace.setCertI(1);
            duoduoUserSpace.setPhone(phone);
            duoduoUserSpace.setName(name);
            duoduoUserSpace.setMyIdCard(myIdCard);
            duoduoUserSpace.setUserId(searchDto.getAppUserId().intValue());
            Random random=new Random();
            int  i=random.nextInt(10);
            duoduoUserSpace.setParkingSpace("b"+i);
             int result= duoduoUserSpaceMapper.insert(duoduoUserSpace);
            if (ValidateUtils.isGreaterThan0(result)) {
                resultDto.setCode(GlobalError.C11031);
                resultDto.setMsg(GlobalError.E11031);
            } else {
                resultDto.setCode(GlobalError.C11038);
                resultDto.setMsg(GlobalError.E11038);
            }

        } catch (RuntimeException e) {
            e.printStackTrace();
            resultDto.setCode(ErrorCode.C10013);
            resultDto.setMsg(ErrorCode.E10013);
        }
        return resultDto;
    }

    @Override
   public ResultDto certfMyName(SearchDto searchDto) {
       //现在一期认证车位给的是假数据
       ResultDto resultDto = new ResultDto();
       searchDto.setPageNo((searchDto.getPageNo() - 1) * searchDto.getPageSize());
       JSONObject filed = searchDto.getFiled();
       try {
           EntityWrapper entityWrapper1 = new EntityWrapper();
           entityWrapper1.where("id={0} and userAuth=1",searchDto.getAppUserId());//searchDto.getAppUserId()
           List<DuoduoUser> lists = duoduoUserMapper.selectList(entityWrapper1);
           if (lists.size()>0) {
               resultDto.setMsg("用户已认证");
           }else{
                 resultDto.setCode(GlobalError.C11037);
                 resultDto.setMsg(GlobalError.E11037);

           }
       } catch (RuntimeException e) {
           e.printStackTrace();
           resultDto.setCode(ErrorCode.C10013);
           resultDto.setMsg(ErrorCode.E10013);
       }
       return resultDto;
   }
    @Override
    public ResultDto saleMyParking(SearchDto searchDto) {
        ResultDto resultDto = new ResultDto();
        JSONObject filed = searchDto.getFiled();
        searchDto.setPageNo((searchDto.getPageNo() - 1) * searchDto.getPageSize());
        try {
            if (!filed.has("id")) {
                resultDto.setMsg(ErrorCode.E10011);
                resultDto.setCode(ErrorCode.C10011);
                return resultDto;
            } else {
                int id = filed.getInt("id");
                //时间转成时间戳
                String start = filed.getString("startTime");
                String end = filed.getString("endTime");
                filed.put("startTime",DateUtils.getStringToInt_seconds(start,DateUtils.PATTERN_DATE_TIME));
                filed.put("endTime",DateUtils.getStringToInt_seconds(end,DateUtils.PATTERN_DATE_TIME));
               // filed.put("id",id);
                DuoduoUserSpace duoduoUserSpace = (DuoduoUserSpace) JSONObject.toBean(filed,DuoduoUserSpace.class);
                duoduoUserSpace.setIsRelease(1);
                duoduoUserSpace.setCertI(2);
               // duoduoUserSpace.setParkingSpace("B9");
                int result = duoduoUserSpaceMapper.updateById(duoduoUserSpace);
                if (ValidateUtils.isGreaterThan0(result)) {
                    resultDto.setMsg("发布成功");
                } else {
                    resultDto.setMsg(ErrorCode.E11003);
                    resultDto.setCode(ErrorCode.C11003);
                }
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
            resultDto.setCode(ErrorCode.C10013);
            resultDto.setMsg(ErrorCode.E10013);
        }
        return resultDto;
    }

    @Override
    public ResultDto cancleSale(SearchDto searchDto) {
        ResultDto resultDto = new ResultDto();
        JSONObject filed = searchDto.getFiled();
        searchDto.setPageNo((searchDto.getPageNo() - 1) * searchDto.getPageSize());
        try {
            if (!filed.has("id")) {
                resultDto.setMsg(ErrorCode.E10011);
                resultDto.setCode(ErrorCode.C10011);
                return resultDto;
            } else {
                int id = filed.getInt("id");
                DuoduoUserSpace duoduoUserSpace = new DuoduoUserSpace();
                duoduoUserSpace.setId(id);
                duoduoUserSpace.setIsRelease(0);
                //取消出租就是删除isDelete=1
                duoduoUserSpace.setIsDelete(1);
                duoduoUserSpace.setIsAuth(0);
                int result = duoduoUserSpaceMapper.updateById(duoduoUserSpace);
                if (ValidateUtils.isGreaterThan0(result)) {
                    resultDto.setMsg("取消成功");
                } else {
                    resultDto.setMsg(ErrorCode.E11003);
                    resultDto.setCode(ErrorCode.C11003);
                }
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
            resultDto.setCode(ErrorCode.C10013);
            resultDto.setMsg(ErrorCode.E10013);
        }
        return resultDto;
    }

    @Override
    public ResultDto certfMyCarpart(SearchDto searchDto) {
        //现在一期认证车位给的是假数据
        ResultDto resultDto = new ResultDto();
        searchDto.setPageNo((searchDto.getPageNo() - 1) * searchDto.getPageSize());
        try {
            RowBounds rowBounds = new RowBounds(searchDto.getPageNo(), searchDto.getPageSize());
            List<DuoduoCertfCartport> duoduoUserSpaceList = duoduoCertfCartportMapper.selectPage(rowBounds, new EntityWrapper<DuoduoCertfCartport>()
                    .where(" userId={0}",searchDto.getAppUserId()).orderBy("createTime", false));
            if (duoduoUserSpaceList.size()>0){
                if (duoduoUserSpaceList.get(0).getPicAuth()==2) {
                    resultDto.setCode(GlobalError.C11041);
                    resultDto.setMsg(GlobalError.E11041);
                }else if (duoduoUserSpaceList.get(0).getPicAuth()==1){
                    resultDto.setCode(GlobalError.C11040);
                    resultDto.setMsg(GlobalError.E11040);
                }
            }else if(duoduoUserSpaceList.size()<=0 || duoduoUserSpaceList.get(0).getPicAuth()==3){
                resultDto.setCode(ErrorCode.C11001);
                resultDto.setMsg(ErrorCode.E11001);
            }
         }  catch (RuntimeException e) {
            e.printStackTrace();
            resultDto.setCode(ErrorCode.C10013);
            resultDto.setMsg(ErrorCode.E10013);
        }
          return resultDto;
    }
    @Override
    public ResultDto savePicPath(SearchDto searchDto) {
        ResultDto resultDto = new ResultDto();
        JSONObject filed = searchDto.getFiled();
        searchDto.setPageNo((searchDto.getPageNo() - 1) * searchDto.getPageSize());
        try {
            if (!filed.has("url")){
                resultDto.setMsg(ErrorCode.E10011);
                resultDto.setCode(ErrorCode.C10011);
                return resultDto;
            }else{

               String  url= filed.getString("url");
                String timestamp = String.valueOf(new Date().getTime()/1000);
                String time1= Integer.valueOf(timestamp).toString();
              DuoduoCertfCartport  duoduoCertfCartport= new  DuoduoCertfCartport();

                duoduoCertfCartport.setPicAuth(1);//审核中
                duoduoCertfCartport.setCertfPicPath(url);
                duoduoCertfCartport.setCreateTime(time1);
                duoduoCertfCartport.setUserId(searchDto.getAppUserId().intValue());//searchDto.getAppUserId().intValue()

                   int result= duoduoCertfCartportMapper.insert(duoduoCertfCartport);
                    if (ValidateUtils.isGreaterThan0(result)){
                        resultDto.setCode(GlobalError.C11040);
                        resultDto.setMsg(GlobalError.E11040);
                    }else{
                        resultDto.setCode(GlobalError.C11038);
                        resultDto.setMsg(GlobalError.E11038);
                    }
                }


        }catch (RuntimeException e) {
            e.printStackTrace();
            resultDto.setCode(ErrorCode.C10013);
            resultDto.setMsg(ErrorCode.E10013);
        }
        return resultDto;
    }

}

